import java.net.*;
import java.io.*;
import java.util.Hashtable;

class BehaviourLoader extends ClassLoader
{
	public BehaviourLoader(Socket socket) throws IOException {
		this.socket = socket;
		DataInputStream din = null;

		try{
			din = new DataInputStream(socket.getInputStream());
			int size = din.readInt();
			bytecode=new byte[size];
			din.readFully(bytecode);
		}catch(IOException ex) {
			System.out.println(ex);
		}
    }

    private byte[] loadClassData(String name) throws IOException {
        return bytecode;
    }

    public synchronized Class<? extends Behaviour> loadClass(String name) {
		return loadClass(name,false);
	}

    public synchronized Class<? extends Behaviour> loadClass(String name,boolean resolve) {
        Class c=null;

		try{
			byte[] data=loadClassData(name);
			int length=data.length;
			/*
			System.out.print("Define class ");
			if (name != null)
				System.out.print("\""+name+"\"");
			System.out.println("from "+ l +" bytes bytecode");
			*/

			c = defineClass(name, data, 0, length);
		}catch(Exception e){ }

        if (resolve)
            resolveClass(c);

        return c;
    }

    private Socket socket;
    private byte[] bytecode;
}
